﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;

namespace RegexTrainer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void highlinghtTextTag()
        {
            string token=@"\[\w+\]";
            Regex rex = new Regex(token);
            MatchCollection mc = rex.Matches(txtOutput.Text);
            int StartCursorPosition = txtOutput.SelectionStart;
            foreach (Match m in mc)
            {
                int startIndex = m.Index;
                int StopIndex = m.Length;
                txtOutput.Select(startIndex, StopIndex);
                txtOutput.SelectionColor = Color.Green;
                txtOutput.SelectionStart = StartCursorPosition;
                txtOutput.SelectionColor = Color.Black;
            }
        }

        private void btnReplace_Click(object sender, EventArgs e)
        {
            if (txtRegexPattern1.Text == "" || txtInput.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập thông tin");
            }
            else
            {
                string output = txtInput.Text; ;
                //if (chkAC.Checked == false)
                //{
                //    txtOutput.Text = Regex.Replace(txtInput.Text, txtRegexPattern1.Text, txtReplace.Text, RegexOptions.Compiled);
                //    highlinghtTextTag();
                //}
                //else
                //{
                //    txtOutput.Text = Regex.Replace(txtInput.Text, txtRegexPattern1.Text, txtReplace.Text, RegexOptions.Compiled);
                //    highlinghtTextTag();
                //}
                if (checkBox1.Checked == true)
                {
                    output = Regex.Replace(output, txtRegexPattern1.Text, txtReplace.Text, RegexOptions.Compiled);
                   // highlinghtTextTag();
                }
                if (checkBox2.Checked == true)
                {
                    output = Regex.Replace(output, txtRegexPattern2.Text, txtReplace.Text, RegexOptions.Compiled);
                    //highlinghtTextTag();
                }
                if (checkBox3.Checked == true)
                {
                    output = Regex.Replace(output, txtRegexPattern3.Text, txtReplace.Text, RegexOptions.Compiled);
                   // highlinghtTextTag();
                }
                if (checkBox4.Checked == true)
                {
                    output = Regex.Replace(output, txtRegexPattern4.Text, txtReplace.Text, RegexOptions.Compiled);
                   // highlinghtTextTag();
                }
                if (checkBox5.Checked == true)
                {
                    output = Regex.Replace(output, txtRegexPattern5.Text, txtReplace.Text, RegexOptions.Compiled);
                   // highlinghtTextTag();
                }
                txtOutput.Text = output;
                highlinghtTextTag();
            }
        }
    }
}
